var pageii = null;
var grid = null;
$(function() {

	grid = lyGrid({
		pagId : 'paging',
		l_column : [{
			colkey : "name",
			name : "客户名称"
		}, {
			colkey : "delivery",
			name : "收货时间"
		}, {
			colkey : "legend",
			name : "来回里程",
			renderData : function(rowindex,data, rowdata, column) {
				
				if(data!=''&&data!=null){
					return data+"km"
				}else{
					return "<span style='color:red'>无</span>"
				}
			}
		},{
			colkey : "end_compact",
			name : "合同到期日",
			renderData : function(rowindex,data, rowdata, column) {
				if(data!=''&&data!=null){
					return new Date(data).format("yyyy-MM-dd");
				}else{
					return "<span style='color:red'>无</span>"
				}
			}
		},{
			colkey : "discount",
			name : "调价进度"
		},{
			colkey : "discount_tolerance",
			name : "调价确认情况"
		},{
			colkey : "unload_time",
			name : "卸货时间",
			renderData : function(rowindex,data, rowdata, column) {

				if(data!=''&&data!=null){
					return data+"/h"
				}else{
					return "<span style='color:red'>无</span>"
				}	
			}
		},{
			colkey : "tolerance",
			name : "客户公差",
			renderData : function(rowindex,data, rowdata, column) {
				if(data!=''&&data!=null){
					return "<span style='color:green'>±"+data+"g</span>"
				}else{
					return "<span style='color:red'>无</span>"
				}	
			}
		},{
			colkey : "actual_tolerance",
			name : "内部公差",
			renderData : function(rowindex,data, rowdata, column) {
				if(data!=''&&data!=null){
					return "<span style='color:green'>±"+data+"g</span>"
				}else{
					return "<span style='color:red'>无</span>"
				}	
			}
		},{
			colkey : "remake",
			name : "备注"
		},{
			colkey : "basic_no",
			name : "提示",
			renderData : function(rowindex,data, rowdata, column) {
				if(data==0){
					return "<span style='color:red'>暂无客户基本信息</span>"
				}
				return "";
			}
		}],
		jsonUrl : rootPath + '/clientelse/clientelse_findByPage.shtml',
		dymCol:true,
		checkbox : true,
		serNumber : false
	});
	$("#addFun").click("click", function() {
		addAccount();
	});
	$("#delFun").click("click", function() {
		delAccount();
	});
	$("#editFun").click("click", function() {
		editAccount();
	});
	$("#basisFun").click("click", function() {
		basisAccount();
	});
	
	
	
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();
		grid.setOptions({
			data : searchParams
		});
	});
	
});

function delAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/clientelse/clientelse_deleteEntity.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}


function basisAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1) {
		layer.msg("只能选中一个");
		return;
	}
	if(cbox == ""){
		layer.msg("需要选择一个");
		return;
	}
	pageii = layer.open({
		title : "基本信息",
		type : 2,
		area : [ "800px", "100%" ],
		content : rootPath + '/clientelse/clientbasis_edUI.shtml?id=' + cbox
	});
}

function editAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1) {
		layer.msg("只能选中一个");
		return;
	}
	if(cbox == ""){
		layer.msg("需要选择一个");
		return;
	}
	pageii = layer.open({
		title : "编辑信息",
		type : 2,
		area : [ "800px", "100%" ],
		content : rootPath + '/clientelse/clientelse_edUI.shtml?id=' + cbox
	});
}
function addAccount() {
	pageii = layer.open({
		title : "新增账号",
		type : 2,
		area : [ "800px", "100%" ],
		content : rootPath + '/clientelse/clientelse_addUI.shtml'
	});
}